package main;


public class Instruction {

	boolean valid;
	String type;
	String format;
	int source1;
	int dest;
	int destOriginal;
	int source2;
	int immediate;
	int noOfCycles;
	int N;
	Integer v1,v2;
	int turn;
	int start,finish;
	boolean finished;
	boolean inExecution;

	public Instruction(String type, int dest, Integer source1, Integer source2,
			int immediate, int noOfCycles, String format) {
		
		this.type = type;
		this.source1 = source1;
		this.dest = dest;
		this.destOriginal = dest;
		this.source2 = source2;
		this.immediate = immediate;
		this.noOfCycles = noOfCycles;
		this.N = 0;
		this.turn  = 0;
		this.format = format;
		finished = false;
		inExecution = false;
		valid = true;
		v1 = v2 = 0;
	}
	
	public Instruction(int i) {
		// TODO Auto-generated constructor stub
	}

	public String toString() {
		return type+""+turn;
	}
	
	public boolean isArithmetic() {
		return (type.equals("ADD") || type.equals("ADDI") || type.equals("NAND") ||
				type.equals("MUL") || type.equals("DIV"));	
		
	}
	
	public static boolean isArithmetic(String type) {
		return (type.equals("ADD") || type.equals("ADDI") || type.equals("NAND") ||
				type.equals("MUL") || type.equals("DIV"));	
		
	}
	
	public boolean isMemory() {
		return (type.equals("LW") || type.equals("SW"));	
		
	}
	
	public static boolean isMemory(String type) {
		return (type.equals("LW") || type.equals("SW"));	
		
	}
	
	public boolean isBranch() {
		return !(isArithmetic() || isMemory());	
		
	}
	
	public Instruction clone() {
		
		return new Instruction(type, dest, source1, source2,
			 immediate, noOfCycles, format);
	
	}

	public Instruction reformat() {
		
		format = type+"  ";
		
		if(isArithmetic() && !type.equals("ADDI")) 
			format += "R"+dest+", "+"R"+source1+", "+"R"+source2;
		else if(type.equals("ADDI") || type.equals("LW"))
			format += "R"+dest+", "+"R"+source1+", "+"#"+immediate;
		else if(type.equals("SW") || type.equals("BEQ"))
			format += "R"+source1+", "+"R"+source2+", "+"#"+immediate;
		else if(type.equals("JALR"))
			format += "R"+source1+", "+"R"+source2;
		else if(type.equals("JMP"))
			format += "R"+source1+", "+"#"+immediate;
		else format += "R"+source1;
			
		
		return this;
	}
	
}
